﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace libVNEditor
{
    public class CLabel : Form
    {
        private TableLayoutPanel tableLayoutPanel1;
        private Label label1;
        private TextBox textBox_LabelName;
        private TableLayoutPanel tableLayoutPanel2;
        private Button btn_CreateSentence;
        private Button btn_DeleteSentence;
        private Button btn_MoveDown;
        private Button btn_MoveUp;
        private ListBox listBox_sentences;
        //所有句子的列表
        public List<CSentence> sentences = new List<CSentence>();
        public string LabelName
        {
            set
            {
                textBox_LabelName.Text = value;
            }
            get
            {
                return textBox_LabelName.Text;
            }
        }

        public override string ToString()
        {
            string str;
            str = "<label name=\"" + LabelName + "\">\n";
            foreach(CSentence sentence in sentences)
            {
                str += sentence.ToString();
            }
            str += "</label>";
            return str;
        }

        public CLabel()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox_LabelName = new System.Windows.Forms.TextBox();
            this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
            this.btn_MoveUp = new System.Windows.Forms.Button();
            this.btn_DeleteSentence = new System.Windows.Forms.Button();
            this.btn_MoveDown = new System.Windows.Forms.Button();
            this.btn_CreateSentence = new System.Windows.Forms.Button();
            this.listBox_sentences = new System.Windows.Forms.ListBox();
            this.tableLayoutPanel1.SuspendLayout();
            this.tableLayoutPanel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 2;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.textBox_LabelName, 1, 0);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 1;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(498, 32);
            this.tableLayoutPanel1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(3, 10);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(74, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "标签名";
            // 
            // textBox_LabelName
            // 
            this.textBox_LabelName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_LabelName.Location = new System.Drawing.Point(83, 5);
            this.textBox_LabelName.Name = "textBox_LabelName";
            this.textBox_LabelName.Size = new System.Drawing.Size(412, 21);
            this.textBox_LabelName.TabIndex = 1;
            // 
            // tableLayoutPanel2
            // 
            this.tableLayoutPanel2.ColumnCount = 5;
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel2.Controls.Add(this.btn_MoveUp, 0, 0);
            this.tableLayoutPanel2.Controls.Add(this.btn_DeleteSentence, 0, 0);
            this.tableLayoutPanel2.Controls.Add(this.btn_MoveDown, 1, 0);
            this.tableLayoutPanel2.Controls.Add(this.btn_CreateSentence, 0, 0);
            this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Top;
            this.tableLayoutPanel2.Location = new System.Drawing.Point(0, 32);
            this.tableLayoutPanel2.Name = "tableLayoutPanel2";
            this.tableLayoutPanel2.RowCount = 1;
            this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel2.Size = new System.Drawing.Size(498, 30);
            this.tableLayoutPanel2.TabIndex = 1;
            // 
            // btn_MoveUp
            // 
            this.btn_MoveUp.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_MoveUp.Enabled = false;
            this.btn_MoveUp.Location = new System.Drawing.Point(163, 3);
            this.btn_MoveUp.Name = "btn_MoveUp";
            this.btn_MoveUp.Size = new System.Drawing.Size(74, 24);
            this.btn_MoveUp.TabIndex = 3;
            this.btn_MoveUp.Text = "上移句子";
            this.btn_MoveUp.UseVisualStyleBackColor = true;
            this.btn_MoveUp.Click += new System.EventHandler(this.MoveUpSentenceClick);
            // 
            // btn_DeleteSentence
            // 
            this.btn_DeleteSentence.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_DeleteSentence.Enabled = false;
            this.btn_DeleteSentence.Location = new System.Drawing.Point(83, 3);
            this.btn_DeleteSentence.Name = "btn_DeleteSentence";
            this.btn_DeleteSentence.Size = new System.Drawing.Size(74, 24);
            this.btn_DeleteSentence.TabIndex = 2;
            this.btn_DeleteSentence.Text = "删除句子";
            this.btn_DeleteSentence.UseVisualStyleBackColor = true;
            this.btn_DeleteSentence.Click += new System.EventHandler(this.DeleteSentenceClick);
            // 
            // btn_MoveDown
            // 
            this.btn_MoveDown.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_MoveDown.Enabled = false;
            this.btn_MoveDown.Location = new System.Drawing.Point(243, 3);
            this.btn_MoveDown.Name = "btn_MoveDown";
            this.btn_MoveDown.Size = new System.Drawing.Size(74, 24);
            this.btn_MoveDown.TabIndex = 1;
            this.btn_MoveDown.Text = "下移句子";
            this.btn_MoveDown.UseVisualStyleBackColor = true;
            this.btn_MoveDown.Click += new System.EventHandler(this.MoveDownSentenceClick);
            // 
            // btn_CreateSentence
            // 
            this.btn_CreateSentence.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_CreateSentence.Location = new System.Drawing.Point(3, 3);
            this.btn_CreateSentence.Name = "btn_CreateSentence";
            this.btn_CreateSentence.Size = new System.Drawing.Size(74, 24);
            this.btn_CreateSentence.TabIndex = 0;
            this.btn_CreateSentence.Text = "创建句子";
            this.btn_CreateSentence.UseVisualStyleBackColor = true;
            this.btn_CreateSentence.Click += new System.EventHandler(this.CreateSentenceClick);
            // 
            // listBox_sentences
            // 
            this.listBox_sentences.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listBox_sentences.FormattingEnabled = true;
            this.listBox_sentences.ItemHeight = 12;
            this.listBox_sentences.Location = new System.Drawing.Point(0, 62);
            this.listBox_sentences.Name = "listBox_sentences";
            this.listBox_sentences.Size = new System.Drawing.Size(498, 182);
            this.listBox_sentences.TabIndex = 2;
            this.listBox_sentences.SelectedIndexChanged += new System.EventHandler(this.OnSelectedSentenceChanged);
            this.listBox_sentences.DoubleClick += new System.EventHandler(this.OnEdit);
            // 
            // cLabel
            // 
            this.ClientSize = new System.Drawing.Size(498, 244);
            this.Controls.Add(this.listBox_sentences);
            this.Controls.Add(this.tableLayoutPanel2);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "cLabel";
            this.Text = "标签";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.cLabel_FormClosing);
            this.Load += new System.EventHandler(this.cLabel_Load);
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.tableLayoutPanel2.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        public void RefreshForm()
        {
            listBox_sentences.Items.Clear();
            for (int sentence_count = 0; sentence_count < sentences.Count; sentence_count++)
            {
                CSentence s = sentences[sentence_count];
                listBox_sentences.Items.Add(string.Format("({0}){1}: {2}", (sentence_count + 1).ToString(), s.Speaker, s.plainText));
            }
        }

        private void cLabel_Load(object sender, EventArgs e)
        {
            RefreshForm();
        }

        private void OnSelectedSentenceChanged(object sender, EventArgs e)
        {
            int idx = listBox_sentences.SelectedIndex;
            if(idx == -1)
            {
                btn_MoveUp.Enabled = false;
                btn_MoveDown.Enabled = false;
                btn_DeleteSentence.Enabled = false;
            }
            else if(idx == 0)
            {
                btn_DeleteSentence.Enabled = true;
                if (idx == listBox_sentences.Items.Count - 1)
                {
                    btn_MoveUp.Enabled = false;
                    btn_MoveDown.Enabled = false;
                }
                else if(idx < listBox_sentences.Items.Count - 1)
                {
                    btn_MoveDown.Enabled = true;
                    btn_MoveUp.Enabled = false;
                }
            }
            else if(idx == listBox_sentences.Items.Count - 1)
            {
                btn_DeleteSentence.Enabled = true;
                if (idx == 0)
                {
                    btn_MoveUp.Enabled = false;
                    btn_MoveDown.Enabled = false;
                }
                else if(idx > 0)
                {
                    btn_MoveDown.Enabled = false;
                    btn_MoveUp.Enabled = true;
                }
            }
            else
            {
                btn_DeleteSentence.Enabled = true;
                btn_MoveUp.Enabled = true;
                btn_MoveDown.Enabled = true;
            }
        }

        static string last_speaker = "";
        private void CreateSentenceClick(object sender, EventArgs e)
        {
            /*
            string speaker = Interaction.InputBox("请输入新句子的说话人：", "创建句子", last_speaker);
            if(speaker == "")
            {
                MessageBox.Show("说话人不能为空!");
                return;
            }
            last_speaker = speaker;
            1*/
            CSentence sentence = new CSentence()
            {
                //Speaker = speaker
            };
            sentences.Add(sentence);
            sentences[sentences.Count - 1].ShowDialog();
            RefreshForm();
        }

        private void OnEdit(object sender, EventArgs e)
        {
            int idx = listBox_sentences.SelectedIndex;
            if (idx == -1) return;
            sentences[idx].ShowDialog();
            RefreshForm();
        }

        private void DeleteSentenceClick(object sender, EventArgs e)
        {
            int idx = listBox_sentences.SelectedIndex;
            if (idx == -1) return;
            DialogResult result = MessageBox.Show("确定要删除这个句子吗?", "删除句子", MessageBoxButtons.OKCancel);
            if(result == DialogResult.OK)
            {
                sentences.RemoveAt(idx);
                RefreshForm();
            }
        }

        private void MoveUpSentenceClick(object sender, EventArgs e)
        {
            int idx = listBox_sentences.SelectedIndex;
            CSentence s1 = sentences[idx];
            CSentence s2 = sentences[idx - 1];
            sentences[idx] = s2;
            sentences[idx - 1] = s1;
            RefreshForm();
        }

        private void MoveDownSentenceClick(object sender, EventArgs e)
        {
            int idx = listBox_sentences.SelectedIndex;
            CSentence s1 = sentences[idx];
            CSentence s2 = sentences[idx + 1];
            sentences[idx] = s2;
            sentences[idx + 1] = s1;
            RefreshForm();
        }

        private void cLabel_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Visible = false;
        }
    }
}
